package com.xieyong.mywebsite.controller;

import com.github.pagehelper.PageInfo;
import com.xieyong.mywebsite.model.dto.Result;
import com.xieyong.mywebsite.model.entity.Blog;
import com.xieyong.mywebsite.service.BlogService;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    BlogService blogService;

    @GetMapping("/{id}")
    public Result<Blog> getBlogById(@PathVariable Integer id) {
        return Result.success(blogService.getBlogById(id));
    }

    @GetMapping()
    public Result<List<Blog>> getAllBlog() {
        return Result.success(blogService.getAllBlog());
    }

    @GetMapping("/page")
    public Result<PageInfo<Blog>> getBlogPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startTime,
                              @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime,
                              Blog blog) {
        PageInfo<Blog> blogPageInfo = blogService.getBlogPage(pageNum,pageSize,blog,startTime,endTime);
        return Result.success(blogPageInfo);
    }

    @PostMapping()
    public Result<String> addBlog(@RequestBody @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Blog blog) {
        blogService.insertBlog(blog);
        return Result.success();
    }

    @PutMapping()
    public Result<String> updateBlog(@RequestBody @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Blog blog) {
        blogService.updateBlog(blog);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteBlog(@PathVariable Integer id) {
        blogService.deleteBlog(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result<String> deleteBlogs(@RequestParam List<Integer> ids) {
        blogService.deleteBlogs(ids);
        return Result.success();
    }

}
